package com.ld.poetry.cloudstorage.service;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ld.poetry.cloudstorage.entity.Share;
import com.ld.poetry.cloudstorage.mapper.ShareMapper;
import com.ld.poetry.config.PoetryResult;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.List;

/**
 * 网盘文件信息表业务处理
 **/
@Service
public class ShareService {

    @Resource
    private ShareMapper shareMapper;

    /**
     * 新增
     */
    public void add(Share share) {
        shareMapper.insert(share);
    }

    /**
     * 删除
     */
    public void deleteById(Integer id) {
        shareMapper.deleteById(id);
    }

    /**
     * 批量删除
     */
    public void deleteBatch(List<Integer> ids) {
        for (Integer id : ids) {
            shareMapper.deleteById(id);
        }
    }

    /**
     * 修改
     */
    public void updateById(Share share) {
        shareMapper.updateById(share);
    }

    /**
     * 根据ID查询
     */
    public Share selectById(Integer id) {
        Share share=shareMapper.selectById(id);
        this.packageShare(share);
        return share;
    }


    /**
     * 查询所有
     */
    public List<Share> selectAll(Share share) {
        List<Share> shareList = shareMapper.selectAll(share);
        for (Share s : shareList) {
            this.packageShare(s);
        }
        return shareList;
    }
    private void packageShare(Share share){
        if(share==null){
            return;
        }
        String endTime=share.getEndTime();
        DateTime endDatetime=DateUtil.parseDateTime(endTime);
        DateTime now = new DateTime();
        if(endDatetime.isBeforeOrEquals(now)){
            share.setStatus("已过期");
        }else{
            long offsetHours=DateUtil.parseDateTime(endTime).between(new Date(),DateUnit.HOUR);
            if(offsetHours<24){
                share.setStatus(offsetHours+1+"小时后过期");
            }else{
                int days=BigDecimal.valueOf(offsetHours).divide(BigDecimal.valueOf(24),0,RoundingMode.HALF_UP).intValue();
                share.setStatus(days+"天后过期");
            }
        }
    }


    /**
     * 分页查询
     */
//    public PageInfo<Share> selectPage(Share share, Integer pageNum, Integer pageSize) {
//        PageHelper.startPage(pageNum, pageSize);
//        List<Share> list = shareMapper.selectAll(share);
//        return PageInfo.of(list);
//    }
    public PoetryResult<Page<Share>> selectPage(Share share, Integer pageNum, Integer pageSize) {
        Page<Share> page = new Page<>(pageNum,pageSize);
        List<Share> list = shareMapper.selectAll(share);
        page.setRecords(list);
        page.setTotal(list.size());
        PoetryResult<Page<Share>> result = new PoetryResult<>();
        result.setData(page);
        result.setCode(200);
        result.setMessage("查询成功");
        return result;
    }

}